(define selfmenuroot "<Image>/Script-Fu/Kevin")     ; Change these to suit yourself         
(define selfmenuentry "Make PNG Icon from SVG...")
;
;(define selffileversion "01.09.2014 Version 1.0 - First issue")
(define selffileversion "24.10.2014 Version 1.1 - Add in option to colour icons")

(define selfauthor "Kevin Payne")
(define selfcopyright "Copyright (C) 2014 Kevin Payne paynekj@hotmail.com")

(define selffilename "\n- kp24_png_icon_from_svg.scm")

(define selftipstrip (string-append "Create PNG Icon from SVG source" "\n\n\nRegisters in menu " selfmenuroot  "/" selfmenuentry "\n\nScript File name - " selffilename))

; this is the main routine
(define (script-fu-kp24_png_icon_from_svg source_file size brightness replace_string interactive colorify colour_tint)
  (let* (
          (width size)
          (height size)
          (resolution 96)
          (load-paths 0)
          (contrast 0)       
          (image 0) (drawable 0)
          (buffer (vector "" "" "")) ; used by reg-exp to return pair indicating start and end of match
          (save-file "")
        )
    (if (= interactive TRUE) (gimp-message source_file))
    (set! image (car (file-svg-load RUN-NONINTERACTIVE source_file source_file resolution width height load-paths)))
    (set! drawable (car (gimp-image-get-active-drawable image)))
    (gimp-brightness-contrast drawable brightness contrast)
    
    (if (= colorify TRUE)
      (plug-in-colorify RUN-NONINTERACTIVE image drawable colour_tint)
    )
    (if (= interactive TRUE)
      (begin
        (gimp-display-new image)
        (gimp-displays-flush)
      )
    )
    (if (re-match replace_string source_file buffer)
;      (set! save-file (string-append (substring source_file 0 (car (vector-ref buffer 0))) "-" (number->string size) ".png"))
      (set! save-file (string-append (substring source_file 0 (car (vector-ref buffer 0))) ".png"))
      (error (string-append "File name doesn't match requirement" source_file "\n" replace_string))
    )
    (file-png-save RUN-NONINTERACTIVE image drawable save-file save-file FALSE 9 TRUE FALSE FALSE TRUE TRUE)
    (if (= interactive TRUE)
      (gimp-message save-file)
      (gimp-image-delete image)
    )
  )
)

(define (script-fu-kp24_png_icon_from_svg_batch source_directory size brightness replace_string colorify colour_tint)
  (let* (
          (varFileList (cadr (file-glob (string-append source_directory DIR-SEPARATOR "*.svg") 1) ) )
        )
    (while (not (null? varFileList))
      (let* ((filename (car varFileList)))
        (script-fu-kp24_png_icon_from_svg filename size brightness replace_string FALSE colorify colour_tint)
        (set! varFileList (cdr varFileList))
      )
    )
  )
)

(script-fu-register "script-fu-kp24_png_icon_from_svg"
		    selfmenuentry
		    selftipstrip
		    selfcopyright
		    selfauthor
		    selffileversion
		    ""
		    SF-FILENAME "Source File" "F:\\gimp\\themes\\symbolic-svg\\document-page-setup-symbolic.svg"
                    SF-VALUE "Size" "22"
                    SF-ADJUSTMENT "Brightness" '(127 -127 127 1 10 0 1)
                    SF-STRING "Replace" "-symbolic.svg"
                    SF-TOGGLE "Interactive" TRUE
                    SF-TOGGLE "Colorify" FALSE
                    SF-COLOR  "Colour" '(128 128 128)
)

(script-fu-menu-register "script-fu-kp24_png_icon_from_svg" selfmenuroot)

(script-fu-register "script-fu-kp24_png_icon_from_svg_batch"
		    "svg->png batch"
		    selftipstrip
		    selfcopyright
		    selfauthor
		    selffileversion
		    ""
		    SF-DIRNAME "Source File" "F:\\gimp\\themes\\symbolic-svg\\artlibre\\scalable\\preferences"
                    SF-VALUE "Size" "22"
                    SF-ADJUSTMENT "Brightness" '(127 -127 127 1 10 0 1)
                    SF-STRING "Replace" "-symbolic.svg"
                    SF-TOGGLE "Colorify" FALSE
                    SF-COLOR  "Colour" '(128 128 128)
)
(script-fu-menu-register "script-fu-kp24_png_icon_from_svg_batch" selfmenuroot)
